<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\FunctionController;
use App\Http\Controllers\PublicController;
use App\Models\AuthRelation;
use App\Models\AuthRole;
use App\Models\Integral;
use App\Models\User;
use App\Models\UserIntegral;
use App\Utils\CodeUtil;
use App\Utils\ConstantUtil;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;

class AdminController extends Controller
{
    public function index(Request $request){
        $select['name'] = trim($request->input("name"));
        $obj = User::where("type",ConstantUtil::USER_ADMIN)->orderBy("id","desc");
        if (!empty($select['name']))   $list = $obj->where("name", "LIKE", '%' . $select['name'] . '%');
        $list = $obj->paginate(ConstantUtil::PAGE_SHOW);
        return view('Api/admin/index',["list"=>$list,"select"=>$select]);
    }

    function create()
    {
        return view('Api/admin/create',[
            'roles'  => AuthRole::get()->toArray(),
            'status' => PublicController::$gx_init
        ]);
    }
    function save(Request $request)
    {
        $user = new User();
        $user->password = FunctionController::password($request->input("password"));
        $user->name     = $request->input("username");
        $user->email    = $request->input("username");
        $user->type     = ConstantUtil::USER_ADMIN;
        $roleIds        = $request->input('role_id');
        $success        = true;
        try {
            if (!empty($roleIds)) {
                $user->save();
                AuthRelation::insertAll($user->id,$roleIds);
            }
        } catch (\Exception $e) {
            $success = false;
        }
        if ($success) {
            return response()->json(['code'=> CodeUtil::SUCCESS,"msg"=> "新增用户成功!"]);
        } else {
            return response()->json(['code'=> CodeUtil::API_ERROR,"msg"=> "新增用户失败!"]);
        }
    }

    /**
     * 显示编辑资源表单页.
     */
    public function edit(Request $request)
    {
        $id = $request->input("id");
        $list = User::find($id);
        if(empty($list)) return response()->view("errors::数据不存在");
        $relation = AuthRelation::where("uid","=",$id)->get()->toArray();
        $items    = [];
        if (!empty($relation)) {
            $items = array_column($relation, 'group_id');
        }

        return view('Api/admin/edit',[
            'roles'  => AuthRole::get()->toArray(),
            'status' => PublicController::$gx_init ,
            'list'  => $list,
            'items' => $items
        ]);
    }

    /**
     * 保存更新的资源
     */
    function update(Request $request)
    {
        $id = $request->input("id");
        $role_id = $request->input("role_id");
        $password = $request->input("password");
        $data["name"] = $request->input("username");
        $data["status"] = $request->input("status");
        if ($password != '') {
            $data["password"]  = FunctionController::password($password);
        }
        User::where("id",$id)->update($data);
        if(!empty($role_id)) {
            AuthRelation::insertAll($id,$role_id);
        }else{
            AuthRelation::where("uid",$id)->delete();
        }
        return response()->json(['code'=> CodeUtil::SUCCESS,"msg"=> "保存成功!"]);
    }

    /**
     * 删除指定资源
     * @param  int $id
     */
    function delete(Request $request)
    {
        $id  = $request->input("id");
        if (!is_array($id)) $id = [$id];
        if(empty($id))  return response()->view("errors::数据不存在");
        User::whereIn("id",$id)->delete();
        AuthRelation::whereIn("uid",$id)->delete();
        return response()->json(['code'=> CodeUtil::SUCCESS,"msg"=> "删除成功!"]);
    }

    /**
     * 修改密码
     * @return mixed
     */
    function securityShow(Request $request)
    {
        return view('Api/admin/security',[
        ]);
    }


    /**
     * 修改密码
     * @return mixed
     */
    function security(Request $request)
    {
        $admin_user = session("admin_user");
        $pass = $request->input("password");
        $pass_new = $request->input("pass");
        $user = User::where("id",$admin_user["id"])->first();
        if(!Hash::check($pass,$user["password"])) return response()->json(['code'=> CodeUtil::API_ERROR,"msg"=> "密码错误"]);
        User::where("id",$admin_user["id"])->update(["password"=>FunctionController::password($pass_new)]);
        return response()->json(['code'=> CodeUtil::SUCCESS,"msg"=> "修改密码成功!"]);
    }

    /**
     * 修改个人资料
     * @return mixed
     */
    function infoShow(Request $request)
    {
        $admin_user = session("admin_user");
        $list = User::find($admin_user["id"]);
        return view('Api/admin/info',[
            "list" => $list
        ]);
    }

    /**
     * 修改个人资料
     * @return mixed
     */
    function info(Request $request)
    {
        $data["sex"] = $request->input("sex");
        $data["mobile"] = $request->input("mobile");
        $data["realname"] = $request->input("true_name");
        $admin_user = session("admin_user");
        User::where("id",$admin_user["id"])->update($data);
        return response()->json(['code'=> CodeUtil::SUCCESS,"msg"=> "保存成功!"]);
    }
}
